package com.example.dzcfinal.SQLite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import android.util.Log;

public class MySQLite extends SQLiteOpenHelper {
    public MySQLite(@Nullable Context context){
        super(context, "dzc.db", null, 2);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE user(u_id INTEGER PRIMARY KEY AUTOINCREMENT ,username VARCHAR(20) UNIQUE,password INTEGER,age INTEGER,sex VARCHAR(20),goods VARCHAR(50))");
        db.execSQL("INSERT INTO user VALUES(1,'admin',123,20,'男',null)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public int findUser(String Username){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("user",null,"username=?",new String[]{Username+""},null,null,null);
        if (cursor.getCount()!=0){
            while (cursor.moveToNext()){
                @SuppressLint("Range") Integer uid = cursor.getInt(cursor.getColumnIndex("u_id"));
                @SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex("username"));
                @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex("password"));
                return uid;
            }
        }
        cursor.close();
        db.close();
        return -1;
    }

    public int checkUser(String Username,String Password){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("user",null,"username=?",new String[]{Username+""},null,null,null);
        if (cursor.getCount()!=0){
            while (cursor.moveToNext()){
                @SuppressLint("Range") Integer uid = cursor.getInt(cursor.getColumnIndex("u_id"));
                @SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex("username"));
                @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex("password"));
                if (Password.equals(password)){
                    return uid;
                }else {
                    return -2;
                }
            }
        }
        cursor.close();
        db.close();
        return -1;
    }

    public void insertUser(String username,String password){
        SQLiteDatabase db = getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("username",username);
        values.put("password",password);
        long id = db.insert("user",null,values);
        Log.i("InsertId", String.valueOf(id));
        db.close();
    }

    @SuppressLint("Range")
    public User getUser(Integer userId){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query("user",null,"u_id=?",new String[]{userId+""},null,null,null);
        User user = new User();
        if (cursor.getCount()!=0){
            while (cursor.moveToNext()){
                user.setUsername(cursor.getString(cursor.getColumnIndex("username")));
                user.setPassword(cursor.getInt(cursor.getColumnIndex("password")));
                user.setAge(cursor.getInt(cursor.getColumnIndex("age")));
                user.setSex(cursor.getString(cursor.getColumnIndex("sex")));

                return user;
            }
        }
        cursor.close();
        db.close();
        return user;
    }

    public void editUser(String oldUsername,User user){
        SQLiteDatabase db = getReadableDatabase();
        String sql = "update user set username= '" +user.getUsername()+"',password='" +user.getPassword()+"',sex='" +user.getSex()+"',age='" +user.getAge()+"' where username='" +oldUsername+"'";
        db.execSQL(sql);
    }
}
